<template>
  <div class="wrapper">
    <div style="margin: 200px auto; background-color: #fff; width: 400px; height: 350px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 30px">易下后台登录</div>
      <el-form :model="loginForm" :rules="rules" ref="userForm">

        <el-form-item prop="username">
          <el-input size="medium" style="margin: 15px 0" prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" style="margin: 15px 0" prefix-icon="el-icon-lock" show-password v-model="loginForm.password"></el-input>
        </el-form-item>

        <el-form-item style="margin: 10px; text-align: right">
          <el-button type="primary" size="medium" autocomplete="off" @click="login">登陆</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在3-12个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 18, message: '长度在6-18个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {
          this.request.post("/admin/user/login", this.loginForm).then(res => {
            if(res.code == 200) {
              localStorage.setItem("token", res.data);  //存储token
              this.$message.success(res.msg);
              this.$router.push("/adminManage/adminHome");
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          return false;
        }
      });

    }
  }
}
</script>

<style>
.wrapper{
  height: 100vh;
  background: url("../assets/登陆背景.jpg");
  background-size:100% 100%;
  overflow: hidden;
}

</style>
